package com.kalvin.kvf.modules.ks.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kalvin.kvf.modules.hxy.entity.Port;
import com.kalvin.kvf.modules.ks.mapper.KSCookieMapper;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.kalvin.kvf.modules.ks.entity.KSLoginUser;
import com.kalvin.kvf.modules.ks.mapper.KSLoginUserMapper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务端登录抖音账号 服务实现类
 * </p>
 * @since 2023-11-13 17:35:38
 */
@Service
public class KSLoginUserServiceImpl extends ServiceImpl<KSLoginUserMapper, KSLoginUser> implements KSLoginUserService {

    @Autowired
    private KSCookieMapper ksCookieMapper;
    @Override
    public Page<KSLoginUser> listLoginUserPage(KSLoginUser loginUser) {
        Page<KSLoginUser> page = new Page<>(loginUser.getCurrent(), loginUser.getSize());
        List<KSLoginUser> loginUsers = baseMapper.selectLoginUserList(loginUser, page);
        return page.setRecords(loginUsers);
    }




    public Map<String,Object> KSuseCookiesLogin(Port port) throws Exception{
        Map<String,Object> map = new HashMap<>();
        //使用dos打开一个浏览器窗口
        Runtime.getRuntime().exec(" C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe --remote-debugging-port="+port.getPort()+" --user-data-dir=\"D:\\chrome"+port.getPort()+"\"");
        ChromeOptions chromeOptions = new ChromeOptions();
        chromeOptions.setExperimentalOption("debuggerAddress", "localhost:"+port.getPort());
        //使用webDriver去连接已经打开的浏览器
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.get("https://www.kuaishou.cn/");
        Thread.sleep(3000);
        //删除上次登录信息
//        driver.manage().deleteAllCookies();
//        Thread.sleep(2000);
//        driver.navigate().refresh();
        //获取抖音账户数据
//        QueryWrapper<LoginUser> userWrapper = new QueryWrapper<>();
//        userWrapper.lambda().eq(LoginUser::getIsLogin, 0);
//        userWrapper.lambda().orderByAsc(LoginUser::getLastLoginTime);
//        userWrapper.last("limit 1");
//        LoginUser loginUser = loginUserMapper.selectOne(userWrapper);

        //没有查到用户信息则返回null
//        if (StringUtils.isEmpty(loginUser)) {
//            map.put("code",-1);
//            map.put("msg","没有可用的登录信息!");
//           return map;
//        }
        //用户登录状态   0：未登录  1：登陆中
        //Integer isLogin = loginUser.getIsLogin();

        //获取账号cookies
//        QueryWrapper<KSCookie> cookieWrapper = new QueryWrapper<>();
//        cookieWrapper.lambda().eq(KSCookie::getLoginUserId, loginUser2.getId());
//        List<KSCookie> ksCookies = ksCookieMapper.selectList(cookieWrapper);

        //使用cookies登录
//        try {
//            for (KSCookie cookie : ksCookies) {
//                if (!StringUtils.isEmpty(cookie.getName()) && !StringUtils.isEmpty(cookie.getValue())) {
//                    Cookie ck = new Cookie(cookie.getName(), cookie.getValue());
//                    driver.manage().addCookie(ck);
//                }
//            }
//            Thread.sleep(4000);
//            //刷新使登录有效
//            driver.navigate().refresh();
//            Thread.sleep(3000);
//        }catch (Exception e){
//            e.printStackTrace();
//            map.put("code",-1);
//            map.put("msg","登陆失败!");
//            return map;
//        }

        //修改该抖音用户的登录状态  为  登陆中
//        loginUser.setIsLogin(isLogin == 0 ? 1 : 0);
//        //更新上次登录时间
//        loginUser.setLastLoginTime(LocalDateTime.now());
//        loginUserMapper.updateById(loginUser);
        //抖音号
        //String accountNumber = loginUser2.getAccountNumber();

        map.put("code",0);
        //map.put("msg",accountNumber);
        map.put("driver",driver);
        return map;
    }

}
